package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.POJO.CheckGroup;
import com.itheima.POJO.CheckItem;
import org.apache.ibatis.annotations.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface checkgroupMapper {

    @Select("SELECT * FROM t_checkitem ")
    public List<CheckItem> findAllCheckitem();

    @Insert("insert into t_checkgroup(code,name,sex,helpCode,remark,attention) values (#{code},#{name},#{sex},#{helpCode},#{remark},#{attention})")
    @SelectKey(keyProperty = "id",statement = "select last_insert_id();",resultType = int.class  ,before = false)
    void addCheckgroup(CheckGroup checkGroup);


    @Insert("insert into t_checkgroup_checkitem(checkgroup_id,checkitem_id) values (#{checkgroup_id},#{checkitem_id})")
    void setCheckGroupAndCheckItem(Map<String, Integer> map);
     /* @Insert("INSERT INTO t_checkgroup_checkitem(checkgroup_id,checkitem_id) VALUES (#{checkgroupId},#{checkitemId})")
    void setCheckGroupAndCheckItem(@Param("checkgroupId") Integer checkgroupId,@Param("checkitemId") Integer checkitemId);*/



    /*
    select * from t_checkgroup
    <where>
       <if test="value !=null and value.length > 0">
         code  = #{value} or name like concat('%',#{value},'%') or helpCode like  concat('%',#{value},'%')
         </if>
    </where>
    */
     @Select("<script>select * from t_checkgroup\n" +
             "    <where>\n" +
             "       <if test=\"value !=null and value.length > 0\">\n" +
             "         code  = #{value} or name like concat('%',#{value},'%') or helpCode like  concat('%',#{value},'%')\n" +
             "        </if>\n" +
             "    </where>" +
             "</script>")
    Page<CheckGroup> findPage(String queryString);


     @Select("SELECT * FROM  t_checkgroup WHERE id =#{id};")
    CheckGroup findCheckgroupById(Integer id);

     @Select("SELECT checkitem_id FROM t_checkgroup_checkitem WHERE checkgroup_id=#{id};")
    Integer[] findCehckitemsByCheckgroupId(Integer id);

     @Delete("delete from t_checkgroup_checkitem where checkgroup_id = #{id}")
    void deleteAssociation(Integer id);


     @Update("<script>update t_checkgroup <set> <if test=\"name != null\"> name = #{name}, </if> <if test=\"sex != null\"> sex = #{sex}, </if> <if test=\"code != null\"> code = #{code}, </if> <if test=\"helpCode != null\"> helpCode = #{helpCode}, </if> <if test=\"attention != null\"> attention = #{attention}, </if> <if test=\"remark != null\"> remark = #{remark}, </if></set> where id = #{id}</script>")
     void updataCheckgroup(CheckGroup formData);

     @Delete("delete  from t_checkgroup where id = #{id}")
    void deleteEdit(Integer id);
}
